//package com.wys.ws_im.controller;
//
//import com.wys.ws_im.exception.FrameErrorCodeEnums;
//import com.wys.ws_im.exception.FrameRuntimeException;
//import com.wys.ws_im.service.UserService;
//import com.wys.ws_im.utils.ResultUtil;
//import com.wys.ws_im.vo.ResultVO;
//import com.wys.ws_im.vo.TokenVO;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.IncorrectCredentialsException;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.session.mgt.SessionKey;
//import org.apache.shiro.subject.Subject;
//import org.apache.shiro.subject.support.DefaultSubjectContext;
//import org.apache.shiro.web.session.mgt.WebSessionKey;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
///**
// * @author wys
// * created in 5:00 2019/5/1
// */
//@Slf4j
//@CrossOrigin
//@RestController
//public class LoginController {
//
//    @Autowired
//    private UserService userService;
//
//    @PostMapping("/login")
//    public ResultVO login(@RequestParam String account, @RequestParam String password
//                            ,HttpServletRequest httpServletRequest
//            , HttpServletResponse httpServletResponse) {
//
//        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
//        Subject subject = SecurityUtils.getSubject();
//
//        Session session = subject.getSession();
//        Object sessionAttribute = session.getAttribute(account);
//        System.out.println("sessionAttribute = " + session.getId());
//        if (sessionAttribute == null) {
//
//            try {
//                subject.login(token);
//                session.setAttribute(token.getUsername(), token.getUsername());
//                // 获取信息
//                TokenVO tokenVO = userService.loginInfo(account);
//                tokenVO.setSession(session.getId().toString());
//                log.info("账号 {} 登录", account);
//                System.out.println("session.getId() = " + session.getId().toString());
//                httpServletResponse.setHeader("Access-Control-Allow-Credentials","true");
//                httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
//                return ResultUtil.success(0, "登录成功", tokenVO);
//            }catch  ( IncorrectCredentialsException ice ) {
//                ice.printStackTrace();
//                // 账号或密码错误420
//                throw new FrameRuntimeException(FrameErrorCodeEnums.ACCOUNT_PASSWORD_ERROR);
//            }catch (AuthenticationException e) {
//                e.printStackTrace();
//                // 账号不存在433
//                throw new FrameRuntimeException(FrameErrorCodeEnums.ACCOUNT_NULL_ERROR);
//            }
//        }
//
//        System.out.println("sessionAttribute = " + sessionAttribute.toString());
//        return ResultUtil.success(0, "已经登录");
//    }
//
//
//    @GetMapping("/logout")
//    public String logout() {
//
//        Subject subject = SecurityUtils.getSubject();
//        if (subject.isAuthenticated()) {
//            subject.logout();
//            return "已经注销";
//        }else {
//            return "未登录";
//
//        }
//    }
//
//    @GetMapping("/test")
//    public String test(String account) {
//
//        Subject subject = SecurityUtils.getSubject();
//        Session session = subject.getSession();
//        Object sessionAttribute = session.getAttribute(account);
//        if (sessionAttribute == null) {
//
//            return "未登录";
//        }else {
//
//            System.out.println("sessionAttribute = " + sessionAttribute.toString());
//            return "已经登录";
//        }
//    }
//
//    @GetMapping("/session")
//    public String session(String session, HttpServletRequest request, HttpServletResponse response) {
//        boolean status = false;
//        SessionKey key = new WebSessionKey(session,request,response);
//        try{
//
//            Session se = SecurityUtils.getSecurityManager().getSession(key);
//            Object obj = se.getAttribute(DefaultSubjectContext.AUTHENTICATED_SESSION_KEY);
//            if(obj != null){
//                status = (Boolean) obj;
//                System.out.println("se.getId() = " + se.getId().toString());
//                return "已经登录";
//            }
//        }catch(Exception e){
//            e.printStackTrace();
//        }finally{
//            Session se = null;
//            Object obj = null;
//        }
//
//        return "未登录";
//    }
//
//    @GetMapping("/isOne")
//    public String isOne(String account) {
//
//        Subject currentUser = SecurityUtils.getSubject();
//        Session session = currentUser.getSession();
//        System.out.println("session.getId() = " + session.getId());
//        Object attribute = SecurityUtils.getSubject().getSession().getAttribute(account);
//        System.out.println("attribute = " + attribute.toString());
//
//        if (!currentUser.isAuthenticated()) {
//
//            return "未登录" + session.getId().toString();
//        }else {
//
//            return "已经登录" + session.getId().toString();
//        }
//
//    }
//
//
//}
